﻿using PLCS.Domain.PLCConfigs;
using PLCS.EntityFrameworkCore.EntityFrameworkCore;

namespace PLCS.EntityFrameworkCore.PlcConfigs;

public class PLCConfigRepository : EfCoreRepository<PLCSDbContext, PLCConfig, Guid>, IPLCConfigRepository
{
    public PLCConfigRepository(IDbContextProvider<PLCSDbContext> dbContextProvider) : base(dbContextProvider)
    {
    }

    public async Task<PLCConfig?> FindByAddressAsync(string address)
    {
        return await FindAsync(x => x.IPAddress == address);
    }

    public async Task<PLCConfig?> FindByPLCNoAsync(string pLCNo)
    {
        return await FindAsync(x => x.PLCNo == pLCNo);
    }

    public async Task<IEnumerable<PLCConfig>> GetListInUseWithDetailsAsync()
    {
        var query = await WithDetailsAsync(x => x.PLCReadDatas);
        query = query.Where(x => x.IsUse == YesNo.是);
        var entities = await AsyncExecuter.ToListAsync(query);
        return entities;
    }

    public async Task<IEnumerable<PLCConfig>> GetListWithDetailsAsync()
    {
        var query = await WithDetailsAsync(x => x.PLCReadDatas);
        var entities = await AsyncExecuter.ToListAsync(query);
        return entities;
    }
}